/*
 * Copyright (C) 2014 Lucas Rocha
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.meij.fragment;

import java.util.ArrayList;

import org.json.JSONObject;
import org.lucasr.twowayview.ItemClickSupport;
import org.lucasr.twowayview.ItemClickSupport.OnItemClickListener;
import org.lucasr.twowayview.widget.DividerItemDecoration;
import org.lucasr.twowayview.widget.TwoWayView;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.widget.SlidingPaneLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;

import com.fragment.BaseFragment;
import com.meij.R;
import com.meij.activity.MainActivity;
import com.meij.activity.user.UserCenterActivity;
import com.meij.adapter.MainAdapter;
import com.meij.bean.Avatar;
import com.meij.http.Json;
import com.meij.http.Url;

public class MainFragment extends BaseFragment implements OnRefreshListener, OnCheckedChangeListener, OnItemSelectedListener {
	private TwoWayView mRecyclerView;
	private ArrayList<Avatar> avatars = new ArrayList<Avatar>();
	private MainAdapter adapter;
	private MainActivity activity;
	private String sort = "", sex = "", prop = "", online = "", city = "";
	public SlidingPaneLayout paneLayout;
	public SwipeRefreshLayout swipeRefreshLayout;

	@Override
	protected int getID() {
		return R.layout.fragment_main;
	}

	@Override
	public void onResume() {
		super.onResume();
		getData();
	}

	@Override
	protected void editView() {
		activity = (MainActivity) getActivity();
		editList();
		paneLayout = (SlidingPaneLayout) viewGroup.findViewById(R.id.sliding_pane_layout);
		swipeRefreshLayout = (SwipeRefreshLayout) viewGroup.findViewById(R.id.swipe_refresh_widget);
		swipeRefreshLayout.setOnRefreshListener(this);
		editGroup();
	}

	/***
	 * 列表
	 */
	private void editList() {
		mRecyclerView = (TwoWayView) aq.id(R.id.list).getView();
		mRecyclerView.setHasFixedSize(true);
		mRecyclerView.setLongClickable(true);
		final ItemClickSupport itemClick = ItemClickSupport.addTo(mRecyclerView);
		itemClick.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(RecyclerView parent, View child, int position, long id) {
				Avatar avatar = avatars.get(position);
				Intent intent = new Intent(activity, UserCenterActivity.class);
				intent.putExtra("id", avatar.getId());
				startActivity(intent);
			}
		});
		final Drawable divider = getResources().getDrawable(R.drawable.divider);
		mRecyclerView.addItemDecoration(new DividerItemDecoration(divider));
		adapter = new MainAdapter(activity, avatars);
		mRecyclerView.setAdapter(adapter);
	}

	/***
	 * 筛选
	 */
	private void editGroup() {
		RadioGroup rg1 = (RadioGroup) viewGroup.findViewById(R.id.rg1);
		rg1.setOnCheckedChangeListener(this);
		RadioGroup rg2 = (RadioGroup) viewGroup.findViewById(R.id.rg2);
		rg2.setOnCheckedChangeListener(this);
		RadioGroup rg3 = (RadioGroup) viewGroup.findViewById(R.id.rg3);
		rg3.setOnCheckedChangeListener(this);
		Spinner spinner1 = (Spinner) viewGroup.findViewById(R.id.spinner1);
		spinner1.setOnItemSelectedListener(this);
		Spinner spinner2 = (Spinner) viewGroup.findViewById(R.id.spinner2);
		spinner2.setOnItemSelectedListener(this);
		aq.id(R.id.button1).clicked(new OnClickListener() {
			@Override
			public void onClick(View v) {
				paneLayout.closePane();
				getData();
			}
		});
	}

	public void getData() {
		if (mRecyclerView == null) {
			return;
		}
		baseCallBack callBack = new baseCallBack() {
			@Override
			public void onStart() {
				swipeRefreshLayout.setRefreshing(true);
			}

			@Override
			public void onSuccess(JSONObject object) {
				super.onSuccess(object);
				avatars.clear();
				avatars.addAll(Json.getAvatars(object));
				adapter.notifyDataSetChanged();
				swipeRefreshLayout.setRefreshing(false);
			}

			@Override
			public void onFailure(String message) {
				super.onFailure(message);
				swipeRefreshLayout.setRefreshing(false);
			}
		};
		String url = Url.wall + "?latitude=" + activity.latitude + "&longitude=" + activity.longitude + sort + sex + prop + online + city;
		activity.getJson(url, callBack);
	}

	@Override
	public void onRefresh() {
		getData();
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.rb1:
			sex = "&filter.sex=1";
			break;
		case R.id.rb2:
			sex = "&filter.sex=2";
			break;
		case R.id.rb3:
			sex = "";
			break;
		case R.id.rb4:
			city = "&filter.city=1";
			break;
		case R.id.rb5:
			city = "";
			break;
		case R.id.rb6:
			online = "&filter.online=1";
			break;
		case R.id.rb7:
			online = "";
			break;
		}
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
		switch (parent.getId()) {
		case R.id.spinner1:
			switch (position) {
			case 0:
				prop = "&filter.prop=1";
				break;
			case 1:
				prop = "&filter.prop=2";
				break;
			case 2:
				prop = "&filter.prop=3";
				break;
			case 3:
				prop = "&filter.prop=23";
				break;
			case 4:
				prop = "";
				break;
			}
			break;
		case R.id.spinner2:
			sort = "&sort=" + (position + 1);
			break;
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {

	}
}
